package com.douqu.game.core.entity;

import com.douqu.game.core.e.E_SkillType;
import com.douqu.game.core.entity.battle.BattleAObject;
import com.douqu.game.core.entity.battle.Position;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.Utils;

/**
 * Created by bean on 2017/7/27.
 */
public class Skill extends GameObject {

    public String icon;

    public String action;

    public String skillScript;

    /**
     * 类型
     * 1。主将技能
     * 2。出场技能
     * 3。死亡技能
     * 4。周期技能
     * 5。普攻技能
     */
    public int type;

    public int cdTime;

    public int crystal;

    /** 效果范围
     * 类型：1.圆  参数 半径   2.方形 参数 长、宽  3.单体锁敌 参数 距离  4.群体圆形锁敌 参数 距离 半径
     * */
    public int[] effectRange;

    /**
     * 效果类型
     * 1.调用物理攻击进行伤害
     * 2.调用穿刺攻击进行伤害
     * 3.调用魔法攻击进行伤害
     * 4.一次性加血
     * 5.持续加血
     */
    public int effectType;

    /**
     * 效果值
     */
    public int effectValue;

    /**
     * 作用对象
     * 1。敌方
     * 2。友方
     * 3。全局
     * 4。自己
     */
    public int effectTargetType;

    /**
     * 作用单位类型
     * 1。地面
     * 2。飞行
     */
    public int effectUnitType;

    /**
     *
     */
    public int buffId;

    /**
     * 技能系数
     */
    public double ratio;

    /** 技能效果动作时间(单位：毫秒) */
    public int actionTime;

    /**
     * 技能释放方式(客户端用)
     */
    public int releaseType;

    /**
     * 等级
     */
    public int lv;


    @Override
    public void copyTo(GameObject gameObject)
    {
        super.copyTo(gameObject);

        Skill skill = (Skill) gameObject;
        skill.icon = icon;
        skill.action = action;
        skill.type = type;
        skill.cdTime = cdTime;
        skill.crystal = crystal;
        skill.effectRange = effectRange;
        skill.effectType = effectType;
        skill.effectValue = effectValue;
        skill.effectTargetType = effectTargetType;
        skill.effectUnitType = effectUnitType;
        skill.buffId = buffId;
        skill.ratio = ratio;
        skill.actionTime = actionTime;
        skill.releaseType = releaseType;
        skill.lv = lv;
    }



    /**
     * 是否是伤害型技能
     * @return
     */
    public E_SkillType getType()
    {
        switch (effectType)
        {
            case ConstantFactory.EFFECT_TYPE_HP_SUB_PHY_ATK:
            case ConstantFactory.EFFECT_TYPE_HP_SUB_PUN_ATK:
            case ConstantFactory.EFFECT_TYPE_HP_SUB_MAG_ATK:
                return E_SkillType.HP_SUB;
            case ConstantFactory.EFFECT_TYPE_HP_ADD:
                return E_SkillType.HP_ADD;
        }

        return E_SkillType.NONE;
    }


    /**
     * 检测技能是否能对此单位有效果
     * @param target
     * @return
     */
    public boolean checkUnitType(BattleAObject target)
    {
        //检测是否是自己的攻击类型
        if(effectUnitType == ConstantFactory.UNIT_TYPE_ALL)
        {
            return true;
        }

        return effectUnitType == target.unitType;
    }

    public boolean checkTargetType(BattleAObject source, BattleAObject target)
    {
        if(effectTargetType == ConstantFactory.TARGET_TYPE_ALL)
            return true;

        if(effectTargetType == ConstantFactory.TARGET_TYPE_SELF)
            return source.getId() == target.getId();

        if(effectTargetType == ConstantFactory.TARGET_TYPE_ENEMY)
            return source.getTeamNo() != target.getTeamNo();

        if(effectTargetType == ConstantFactory.TARGET_TYPE_PARTNER)
            return source.getTeamNo() == target.getTeamNo();

        return false;
    }

    /**
     * 是否是只能对单体进行攻击
     * @return
     */
    public boolean isSingleTarget()
    {
        return effectRange[0] == ConstantFactory.RANGE_TYPE_SINGLE_LOCK;
    }

    /**
     * 是否在攻击范围内
     * @param source
     * @param target
     * @return
     */
    public boolean isInAtkRange(BattleAObject source, BattleAObject target)
    {
        //圆形
        if(effectRange[0] == ConstantFactory.RANGE_TYPE_CIRCLE)
        {
//            System.out.println("Skill isInAtkRange 检测圆形范围:" + effectRange[1]);
            return Utils.isInCircleRange(source.position, target.position, effectRange[1]);
        }
        //矩形
        else if(effectRange[0] == ConstantFactory.RANGE_TYPE_RECTANGLE)
        {
//            System.out.println("Skill isInAtkRange 检测矩形范围:" + effectRange[1] + "++++" + effectRange[2]);
            return Utils.isInRectangleRange(source.getTeamNo(), source.position, target.position, effectRange[1], effectRange[2]);
        }
        //单体锁敌//群体圆形锁敌
        else if(effectRange[0] == ConstantFactory.RANGE_TYPE_SINGLE_LOCK || effectRange[0] == ConstantFactory.RANGE_TYPE_GROUP_CIRCLE_LOCK)
        {
//            System.out.println("Skill isInAtkRange 检测单体锁敌,群体圆形锁敌:" + effectRange[1]);
            return Utils.isInCircleRange(source.getPosition(), target.getPosition(), effectRange[1]);
        }

        return false;
    }

    public boolean isInAtkRange(Position source, int teamNo, BattleAObject target)
    {
        //圆形
        if(effectRange[0] == ConstantFactory.RANGE_TYPE_CIRCLE)
        {
//            System.out.println("Skill isInAtkRange 检测圆形范围:" + effectRange[1]);
            return Utils.isInCircleRange(source, target.position, effectRange[1]);
        }
        //矩形
        else if(effectRange[0] == ConstantFactory.RANGE_TYPE_RECTANGLE)
        {
//            System.out.println("Skill isInAtkRange 检测矩形范围:" + effectRange[1] + "++++" + effectRange[2]);
            return Utils.isInRectangleRange(teamNo, source, target.position, effectRange[1], effectRange[2]);
        }
        //单体锁敌//群体圆形锁敌
        else if(effectRange[0] == ConstantFactory.RANGE_TYPE_SINGLE_LOCK || effectRange[0] == ConstantFactory.RANGE_TYPE_GROUP_CIRCLE_LOCK)
        {
//            System.out.println("Skill isInAtkRange 检测单体锁敌,群体圆形锁敌:" + effectRange[1]);
            return Utils.isInCircleRange(source, target.getPosition(), effectRange[1]);
        }

        return false;
    }



    /**
     * 是否是锁敌技能
     * @return
     */
    public boolean isLockTarget()
    {
        return effectRange[0] == ConstantFactory.RANGE_TYPE_GROUP_CIRCLE_LOCK || effectRange[0] == ConstantFactory.RANGE_TYPE_SINGLE_LOCK;
    }


//    /**
//     * 检测是否在视野范围内
//     * @param source
//     * @param target
//     * @return
//     */
//    public boolean isInVisionRange(BattleAObject source, BattleAObject target)
//    {
//        //检测是否是自己的攻击类型
//        if(effectUnitType != ConstantFactory.UNIT_TYPE_ALL)
//        {
//            if(effectUnitType != target.unitType)
//                return false;
//        }
//
//        return Utils.isInCircleRange(source.position, target.position, effectRange[1]);
//    }


    @Override
    public void setVariable(String key, String value)
    {
        if("effectRange".equals(key))
        {
            effectRange = LoadUtils.loadIntArray(key, value);
        }
        else if("actionTime".equals(key))
        {
            this.actionTime = Integer.parseInt(value);
            AnimEvent animEvent = DataFactory.getInstance().getGameObject(DataFactory.SOLDIER_ANIM_KEY, this.id);
            if(animEvent != null)
            {
                double temp = (double)animEvent.attackEventPoint / 30 * 1000;
                this.actionTime += temp;
            }
        }
        else
            super.setVariable(key, value);
    }


}


